﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class EditUnitTrustSaleModel
    {
        public UnitTrustTransactionScreenData ScreenData { get; set; }
        public UnitTrustHeader Header { get; set; }
        public UnitTrustTransaction Transaction { get; set; }
        public EditUnitTrustSaleModel()
        {
            Init();
        }
        void Init()
        {
            if (null == Header)
                Header = new UnitTrustHeader();
            if (null == Transaction)
                Transaction = new UnitTrustTransaction();
            var defaultItem = WebUtil.SelectListSelectDefault();
            TransactionTypeList = new BindingList<SelectListItem> ();
            TypeOfPaymentList = new BindingList<SelectListItem> { defaultItem };
        }

        public static EditUnitTrustSaleModel BuildModel(UnitTrustTransactionScreenData data)
        {
            var model = new EditUnitTrustSaleModel {ScreenData = data};
            if (data.HeaderDTO.Header != null)
                model.Header = data.HeaderDTO.Header;
            if (data.Transaction != null)
                model.Transaction = data.Transaction;

            //fixed value ตัวเลือก "Purchase", "Subscription"  Default = Purchase
            model.TransactionTypeList.Add(item: new SelectListItem() { Selected = false, Text = "Sale", Value = "Sale" });
            //model.TransactionTypeList.Add(item: new SelectListItem() { Selected = false, Text = "Subscription", Value = "Subscription" });
            model.SelectedTransactionType = "Sale";


            if (data.PaymentTypes != null || data.PaymentTypes.Any())
            {
                foreach (var selectListItem in data.PaymentTypes)
                {
                    model.TypeOfPaymentList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.PaymentTypeName, Value = selectListItem.PaymentTypeId.ToString() });
                }
            }
            model.HeaderID = model.Header.ID;
            model.TransactionID = model.Transaction.ID;
            return model;
        }


        #region dropdownlist data
        //TransactionType
        [Required(ErrorMessage = "โปรดระบุ")]
        public string SelectedTransactionType
        {
            get { return Transaction.TransactionType; }
            set { Transaction.TransactionType = value; }
        }
        public IList<SelectListItem> TransactionTypeList { get; set; }

        //Type of Payment (.TYPEOFPAYMENT_NAME)
        [Required(ErrorMessage = "โปรดระบุ")]
        public string SelectedTypeOfPayment
        {
            get { return Transaction.TypeofpaymentName; }
            set { Transaction.TypeofpaymentName = value; }
        }
        public IList<SelectListItem> TypeOfPaymentList { get; set; }

        #endregion


        #region adapter data
        public int HeaderID
        {
            get
            {
                return Transaction.UnitTrustTransactionHeaderID;
            }

            set
            {
                Transaction.UnitTrustTransactionHeaderID = value;
            }
        }
        public int TransactionID
        {
            get
            {
                return Transaction.ID;
            }

            set
            {
                Transaction.ID = value;
            }
        }

        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Maturity Date")]
        public DateTime? HeaderMaturityDate
        {
            get { return Header.MaturityDate; }
            set { Header.MaturityDate = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public string RefNo
        {
            get { return Transaction.RefNO; }
            set { Transaction.RefNO = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Trade Date")]
        public DateTime? TradeDate
        {
            get { return Transaction.TradeDate; }
            set { Transaction.TradeDate = value; }
        }
        [Required(ErrorMessage = "โปรดระบุ")]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Settlement Date")]
        public DateTime? SettlementDate
        {
            get { return Transaction.SettlementDate; }
            set { Transaction.SettlementDate = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public int? Unit
        {
            get { return Transaction.Unit; }
            set { Transaction.Unit = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? PriceUnit
        {
            get { return Transaction.PriceUnit; }
            set { Transaction.PriceUnit = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? ComRate
        {
            get { return Transaction.ComRate; }
            set { Transaction.ComRate = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? VatRate
        {
            get { return Transaction.VatRate; }
            set { Transaction.VatRate = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? WHTaxRate
        {
            get { return Transaction.WHTaxRate; }
            set { Transaction.WHTaxRate = value; }
        }

        #endregion


        public String SubmitBtn { get; set; }
        public String CancelBtn { get; set; }
        public bool UpdateMode { get; set; }

    }
}